var gulp = require('gulp'),
    browserSync = require('browser-sync'),
    pngquant = require('imagemin-pngquant'),
    plugins = require('gulp-load-plugins')();

gulp.task('default', ['js-zip', 'sass-css','html-zip','js-lib-zip','server'], function () {//'png-jpg-zip',
    browserSync.init({
        server: {
            baseDir: './dist'
        }
    });
});

gulp.task('server', function () {
    gulp.watch("src/**",['js-zip', 'sass-css','html-zip','png-jpg-zip','js-lib-zip']).on('change', browserSync.reload);
});

/**
 * @desc 压缩html
 */
gulp.task('html-zip', function () {
    return gulp.src('src/*.html')
        .pipe(plugins.minifyHtml())
        .pipe(gulp.dest('dist'))
});
/**
 * @desc png和jpg压缩(压缩率高)
 */
gulp.task('png-jpg-zip', function (cb) {
    return gulp.src(['src/img/*.png', 'src/img/*.jpg'])
        .pipe(plugins.smushit({
            verbose: true
        }))
        .pipe(gulp.dest('dist/img'));
});
/**
 * @desc 全格式图片压缩(压缩率小)
 */
gulp.task('pic-zip', function () {
    return gulp.src('src/img/*')
        .pipe(plugins.imagemin({
            progressive: true,
            use: [pngquant()] //使用pngquant来压缩png图片
        }))
        .pipe(gulp.dest('dist/img'));
});

/**
 * @desc 检查js
 */
gulp.task('js-lint', function () {
    return gulp.src(['./src/js/**/*.js', '!./src/js/lib/**'])
        .pipe(plugins.jshint())
        .pipe(plugins.jshint.reporter()); // 输出检查结果
});

gulp.task('js-lib-zip', function () {
    return gulp.src('./src/js/lib/*')
        .pipe(plugins.uglify())
        .pipe(gulp.dest('dist/js/lib'));
});

/**
 * @desc 压缩js
 */
gulp.task('js-zip', function () {
    return gulp.src(['./src/js/**/*.js', '!./src/js/lib/**'])
        // .pipe(plugins.rename('common.js'))//重命名文件
        .pipe(plugins.concat('common.js'))
        .pipe(plugins.uglify())
        .pipe(gulp.dest('dist/js'))
});
/**
 * @desc 编译sass并压缩css
 */
gulp.task('sass-css', function () {
    return gulp.src('src/sass/**')
        .pipe(plugins.sass())
        .pipe(plugins.concat('common.css'))
        .pipe(plugins.minifyCss())
        .pipe(gulp.dest('dist/css'))
});